www.gusucode.com > 超声波测量以及形成图像 对相关信号进行模拟仿真 > 超声波测量以及形成图像 对相关信号进行模拟仿真/digital holograpy/prog/variance.m

    function [ b ] = variance( a,sz )
%VARIANCE 2-D variance distribution of an array
%       For each element of a 2-D array a, a variance
%  value is calculated and stored in the same element
%  in b. This varience value is varience of the data
%  in a window whose center is that element being
%  considered. sz is the size of the window

error(nargchk(2,2,nargin))
if nargout>1
    error('Too many output arguments')
end
[M,N]=size(a);
b=zeros([M,N]);

av=conv2(a,ones(sz(1),sz(2)),'same')./sz(1)./sz(2);

left=round((sz(2)-1)/2);
right=sz(2)-1-left;
up=round((sz(1)-1)/2);
down=sz(1)-1-up;

for svy=up:-1:-down
    for svx=left:-1:-right
        b=b+abs(av-circshift(a,[svy,svx])).^2;
    end
end